<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

JFX::checkAdminPermsRedirect('core', 'perm_import_template');

$THEME->addBreadcrumb($this->adminUrl.'/import/import-template', $this->lang('ap_title_export_import-template'));

require_once($CONFIG->classesDir.'/PHPQuery/phpQuery.php');


$VIEW->addJs('
$(document).ready(function(){
    $("#jfxcore-import-template-tree-from").treeview({
        persist: "location",
        collapsed: true,
        unique: true
    });
    $("#jfxcore-import-template-tree-from a").click(function(){
        $("#jfx-parent-id-from").get(0).value = this.id.replace("jfx-page_from_", "");
        $("#jfx-disabled-parent-from").get(0).value = $(this).text();
    });
});
');








if(Antz_IntelliForm::submitted('import-template')){
    $error = false;

    $parentid = (int) post('parentFrom');
    if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$parentid}'")==0){
        $error = true;
        JFX::addError($this->lang('invalid_parent_id'));
    }

    if(!file_exists($_FILES['file']['tmp_name'])){
        $error = true;
        JFX::addError($this->lang('file_upload_failed'));
    }

    if(!$error){
        $fc = file_get_contents($_FILES['file']['tmp_name']);
        $dom = phpQuery::newDocument($fc);

        $divs = pq('div');

        if(count($divs->elements)==0){
            $error = true;
            JFX::addError($this->lang('no_div_tags_found'));
        }else{

            foreach($divs->elements as $key=>$value){
                if(trim($value->getAttribute('id')) != ''){
                    $error = true;
					JFX::addError($this->foldLang('cannot_have_id', $v->getAttribute('id')));
                }
            }
        }
        if(!$error){
            $divs = pq('body>div');
            if(count($divs->elements)>1){
                // create a container div for the root-level elements

                $incrementor = $DB->oneValue($CONFIG->dbprefix.'blocks', 'COUNT(*)', "page_id = '{$parentid}' AND workspace_id = 1 AND module_key = 'content'");
                $incrementor++;
                $dbParams = array(
                    'id' => 'jfxpage-'.$parentid.'-content-'.$incrementor,
                    'module_key' => 'content',
                    'module_action' => 'content',
                    'parent_id' => $parentid,
                    'page_id' => $parentid,
                    'workspace_id' => 1,
                    'sorting' => 0,
                    'exec_order' => 0,
                    'page_parent' => 1,
                    'class_name' => '',
                    'is_droppable' => 0,
                    'is_template' => 1,
                    'template' => '',
                    'block_style' => ''
                );
                $parentBlockId = $parentid.'-content-'.$incrementor;
                $isTemplate = false;
                $DB->insert($CONFIG->dbprefix.'blocks', $dbParams);
                JFXAdmin_ImportTemplate_ImportBlocks($parentid, $parentBlockId, pq('body>div'), $isTemplate);
            }else{

                $parentBlockId = $parentid;
                $isTemplate = true;
                JFXAdmin_ImportTemplate_ImportBlocks($parentid, $parentBlockId, pq('body>div'), $isTemplate);
            }
            JFX::addSuccess($this->lang('template_imported_success'));
        }

    }


}





$pagesRecursiveFrom = JFX::getPageTree(0, 1);

$treeFrom = '<ul id="jfxcore-import-template-tree-from">'.JFX_ImportPage_MakeTree($pagesRecursiveFrom, 'jfx-page_from_', 1).'</ul>';



    $parentFrom = array(
        'name' => 'parentFrom',
        'value' => post('parentFrom', 0),
        'type' => 'hidden',
        'id' => 'jfx-parent-id-from',
        'label' => ''
    );
    $parentListFrom = array(
        'type' => 'longtag',
        'tag' => 'div',
        'class' => 'form-field',
        'id' => 'jfx-parent-page-tree-from',
        'content' => '<label>'.$this->lang('page_to_copy_into').'</label>
<div class="input-area">'.$treeFrom.'</div>'
    );
    $selectedParentFrom = array(
        'name' => 'disabled_parent_from',
        'value' => post('disabled_parent_from'),
        'type' => 'text',
        'id' => 'jfx-disabled-parent-from',
        'extra' => array(
            array(
                'command' => 'addAttribute',
                'params' => array(
                    '"disabled"', '"disabled"'
                )
            )
        ),
        'label' => '&nbsp;'
    );









$formFields = array(
    array(
        'type' => 'text',
        'name' => 'title',
        'value' => post('title', $this->lang('default_new_template_title')),
        'label' => $this->lang('title_for_new_template')
    ),
    array(
        'type' => 'file',
        'name' => 'file',
        'value' => '',
        'label' => $this->lang('file_to_import')
    ),
    $parentFrom,
    $parentListFrom,
    $selectedParentFrom,
    array(
        'type' => 'submit',
        'name' => 'submitBtn',
        'value' => $this->lang('import_template_submit'),
        'label' => '&nbsp;'
    )
);


echo '<h1>'.$this->lang('import_template_heading').'</h1>';
echo '<p>'.$this->lang('import_template_description').'</p>';

echo JFX::makeRapidForm($formFields, $this->lang('import_template_legend'), 'import-template');



















